﻿using System.Collections.Generic;

namespace Interactive.Utility.Extension {
    public static class NumExt {
        /// <summary>
        /// 格式化字节数字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string FormatBytesStr(this int bytes){
            if (bytes > 1073741824)
                return ((double) (bytes/1073741824)).ToString("0") + "G";
            if (bytes > 1048576)
                return ((double) (bytes/1048576)).ToString("0") + "M";
            if (bytes > 1024)
                return ((double) (bytes/1024)).ToString("0") + "K";
            return bytes + "Bytes";
        }


        /// <summary>
        /// 得到集合数量
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int GetICollectionCount<T>(this ICollection<T> source){
            if (source == null)
                return 0;
            return source.Count;
        }
    }
}